﻿using System;
using System.Text.RegularExpressions;
using UnityEngine;
/// <summary>
/// 颜色扩展
/// </summary>
public static class ESF_ColorExtendEngine
{
    #region ToRGB 颜色转字十六进制表示法
    /// <summary>
    /// 将颜色转换成十六进制表示法(带#)
    /// </summary>
    /// <param name="_color">颜色</param>
    /// <returns>标签值</returns>
    public static string ESF_ToRGB(this Color _color)
    {
        return _color.OnESF_ToRGB(true);
    }

    /// <summary>
    /// 将颜色转换成十六进制表示法(不带#)
    /// </summary>
    /// <param name="_color">颜色</param>
    /// <returns>标签值</returns>
    public static string ESF_ToRGB_N(this Color _color)
    {
        return _color.OnESF_ToRGB(false);
    }

    /// <summary>
    /// 将颜色转换成十六进制表示法
    /// </summary>
    /// <param name="_color">颜色</param>
    /// <param name="_symbol">是否带#</param>
    /// <returns>标签值</returns>
    static string OnESF_ToRGB(this Color _color, bool _symbol)
    {
        return string.Format("{0}{1}{2}{3}", _symbol ? "#" : "", ((byte)(_color.r * byte.MaxValue)).ToString("X2"), ((byte)(_color.g * byte.MaxValue)).ToString("X2"), ((byte)(_color.b * byte.MaxValue)).ToString("X2"));
    }
    #endregion

    #region ToRGBA 颜色转字十六进制表示法
    /// <summary>
    /// 将颜色转换成十六进制表示法(带#)
    /// </summary>
    /// <param name="_color">颜色</param>
    /// <returns>标签值</returns>
    public static string ESF_ToRGBA(this Color _color)
    {
        return _color.OnESF_ToRGBA(true);
    }

    /// <summary>
    /// 将颜色转换成十六进制表示法(不带#)
    /// </summary>
    /// <param name="_color">颜色</param>
    /// <returns>标签值</returns>
    public static string ESF_ToRGBA_N(this Color _color)
    {
        return _color.OnESF_ToRGBA(false);
    }

    /// <summary>
    /// 将颜色转换成十六进制表示法
    /// </summary>
    /// <param name="_color">颜色</param>
    /// <param name="_symbol">是否带#</param>
    /// <returns>标签值</returns>
    static string OnESF_ToRGBA(this Color _color, bool _symbol)
    {
        return string.Format("{0}{1}{2}{3}{4}", _symbol ? "#" : "", _color.r.ToString("X2"), _color.g.ToString("X2"), _color.b.ToString("X2"), _color.a.ToString("X2"));
    }
    #endregion

    #region ToColor 十六进制表示法转颜色
    /// <summary>
    /// 十六进制表示法转颜色
    /// </summary>
    /// <param name="_color">十六进制表示法</param>
    /// <returns>颜色</returns>
    public static Color ESF_ToColor(this string _color)
    {
        _color = Regex.Replace(_color, @"(#+)([\d|\w]+)", "$2");
        int colorInt32 = Convert.ToInt32(_color, 16);
        float r = (colorInt32 >> 16);
        float g = ((colorInt32 >> 8) & 0x00ff);
        float b = colorInt32 & 0x0000ff;
        return new Color(r / 255, g / 255, b / 255);
    }
    #endregion

    #region ToColorBrightness 改变颜色亮度
    /// <summary>
    /// 改变颜色亮度
    /// 因子<0变间
    /// >0变亮
    /// </summary>
    /// <param name="color">颜色</param>
    /// <param name="factor">因子(-1,1)</param>
    /// <returns>改变后的颜色</returns>
    public static Color ESF_ToColorBrightness(this Color _color, float _factor)
    {
        Color32 c = _color;
        float red = (float)c.r;
        float green = (float)c.g;
        float blue = (float)c.b;
        _factor = Mathf.Clamp(_factor, -1, 1);
        if (_factor < 0)
        {
            _factor = 1 + _factor;
            red *= _factor;
            green *= _factor;
            blue *= _factor;
        }
        else
        {
            red = (255 - red) * _factor + red;
            green = (255 - green) * _factor + green;
            blue = (255 - blue) * _factor + blue;
        }
        c.r = (byte)Mathf.Clamp((int)red, 0, 255);
        c.g = (byte)Mathf.Clamp((int)green, 0, 255);
        c.b = (byte)Mathf.Clamp((int)blue, 0, 255);
        return c;
    }
    #endregion    
}
